//
//  HNSH_TaskCenterCell.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/30.
//

#import "HNSH_TaskCenterCell.h"

@interface HNSH_TaskCenterCell ()

/* levelIcon */
@property (nonatomic, strong) UIImageView *levelIcon;
/* levelLabel */
@property (nonatomic, strong) UILabel *levelLabel;

/* 任务内容详情 */
@property (nonatomic, strong) UILabel *contentLabel;
/* 所需绿色能量 */
@property (nonatomic, strong) UILabel *sxLabel;
/* 奖励绿色能量 */
@property (nonatomic, strong) UILabel *jlLabel;
/* 剩余任务 */
@property (nonatomic, strong) UILabel *syLabel;

/* 兑换 */
@property (nonatomic, strong) UIButton *dhButton;
@end

@implementation HNSH_TaskCenterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self configView];
    }
    return self;
}

- (void)setModel:(HNSH_TaskCenterListModel *)model {
    _model = model;
    [self.levelIcon sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    self.levelLabel.text = [NSString stringWithFormat:@"%@(%@/%@)",model.name,model.already_received_count,model.maximum_receive_count];
    self.contentLabel.text = [NSString stringWithFormat:@"任务内容详情：%@",model.condition_text];
    self.sxLabel.text = [NSString stringWithFormat:@"所需绿色能量：%.2f",[model.price doubleValue]];
    self.jlLabel.text = [NSString stringWithFormat:@"奖励绿色能量：%.2f",[model.yield doubleValue]];
    self.syLabel.text = [NSString stringWithFormat:@"剩余任务：%ld",model.surplus_count];
    
    [self.dhButton setTitle:model.button_text forState:UIControlStateNormal];
    self.dhButton.hidden = NO;
}

- (void)setMyModel:(HNSH_TaskCenterMyListModel *)myModel {
    _myModel = myModel;
    [self.levelIcon sd_setImageWithURL:[NSURL URLWithString:myModel.quest_icon]];
    self.levelLabel.text = myModel.quest_name;
    self.dhButton.hidden = YES;
    //任务状态 1:进行中 2:已完成 3:已过期 4:已删除
    if (myModel.status == 1) {
        self.syLabel.hidden = NO;
        self.contentLabel.text = [NSString stringWithFormat:@"任务内容详情：%@",myModel.quest_condition_text];
        self.sxLabel.text = [NSString stringWithFormat:@"总奖励绿色能量：%.2f",[myModel.max_reward_yield doubleValue]];
        self.jlLabel.text = [NSString stringWithFormat:@"已奖励绿色能量：%.2f",[myModel.receive_yield doubleValue]];
        self.syLabel.text = [NSString stringWithFormat:@"任务时间：%@天",myModel.max_reward_count];
    } else {
        self.syLabel.hidden = YES;
        self.contentLabel.text = [NSString stringWithFormat:@"任务内容详情：%@",myModel.quest_condition_text];
        self.sxLabel.text = [NSString stringWithFormat:@"已奖励绿色能量：%.2f",[myModel.receive_yield doubleValue]];
        self.jlLabel.text = [NSString stringWithFormat:@"完成日期：%@",myModel.expired_time];
    }
}

- (void)configView {
    [self.contentView addSubview:self.levelIcon];
    [self.contentView addSubview:self.levelLabel];
    
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.sxLabel];
    [self.contentView addSubview:self.jlLabel];
    [self.contentView addSubview:self.syLabel];
    
    [self.contentView addSubview:self.dhButton];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(24, 131.5, kScreenWidth-72, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"#E3E3E3"];
    [self.contentView addSubview:line];
}

#pragma mark lazy
- (UIImageView *)levelIcon {
    if (!_levelIcon) {
        _levelIcon = [[UIImageView alloc]initWithFrame:CGRectMake(24, 11, 28, 28)];
        _levelIcon.image = [UIImage imageNamed:@"task_center_home_cell_icon"];
    }
    return _levelIcon;
}

- (UILabel *)levelLabel {
    if (!_levelLabel) {
        _levelLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 11, 200, 28)];
        _levelLabel.text = @"青铜任务";
        _levelLabel.textColor = [UIColor appBlackLabelColor];
        _levelLabel.font = [UIFont boldSystemFontOfSize:16];
    }
    return _levelLabel;
}

- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 48, 200, 16)];
        _contentLabel.text = @"任务内容详情：";
        _contentLabel.textColor = [UIColor appGray666LabelColor];
        _contentLabel.font = [UIFont systemFontOfSize:12];
    }
    return _contentLabel;
}

- (UILabel *)sxLabel {
    if (!_sxLabel) {
        _sxLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 64, 200, 16)];
        _sxLabel.text = @"所需绿色能量：0";
        _sxLabel.textColor = [UIColor appGray666LabelColor];
        _sxLabel.font = [UIFont systemFontOfSize:12];
    }
    return _sxLabel;
}

- (UILabel *)jlLabel {
    if (!_jlLabel) {
        _jlLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 80, 200, 16)];
        _jlLabel.text = @"奖励绿色能量：0";
        _jlLabel.textColor = [UIColor appGray666LabelColor];
        _jlLabel.font = [UIFont systemFontOfSize:12];
    }
    return _jlLabel;
}

- (UILabel *)syLabel {
    if (!_syLabel) {
        _syLabel = [[UILabel alloc]initWithFrame:CGRectMake(24, 96, 200, 16)];
        _syLabel.text = @"剩余任务：0";
        _syLabel.textColor = [UIColor appGray666LabelColor];
        _syLabel.font = [UIFont systemFontOfSize:12];
    }
    return _syLabel;
}

- (UIButton *)dhButton {
    if (!_dhButton) {
        _dhButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _dhButton.frame = CGRectMake(kScreenWidth-48-70, 50, 70, 30);
        [_dhButton setTitle:@"兑换" forState:UIControlStateNormal];
        [_dhButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _dhButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _dhButton.backgroundColor = [UIColor appGreenColor];
        _dhButton.layer.cornerRadius = 15;
        _dhButton.layer.masksToBounds = YES;
        WeakSelf(weakSelf);
        [[_dhButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            if (weakSelf.cellButtonBlock) {
                weakSelf.cellButtonBlock(weakSelf.model);
            }
        }];
    }
    return _dhButton;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
